/*
 * Registers a thumbs-up rating asynchronously and modifies the points displayed
 * on the website accordingly. Moreover, changes the thumbs-up/down link to an
 * "undo" link.
 */
function thumbsUp(tweetId, owningUser, ratingUser, rtCount, points) {
	showLoader();

	$.ajax({
		'url': 'thumbs-up?id=' + tweetId + '&owning=' + owningUser + '&rating=' + ratingUser,
		'success': function(data) {
			var difference = parseInt(data);
			var newPoints = points + difference;
			var operator = '';
			var newPointsDisplay = newPoints;

			if (newPoints < 0) {
				operator = ' - ';
				newPointsDisplay *= -1;
			} else {
				operator = ' + ';
			}

			$('#rating_' + tweetId).html(rtCount + operator + newPointsDisplay + ' = ' + (rtCount + newPoints));
			$('#links_' + tweetId).html(
				'<span>You rated <strong>+1</strong> </span>' +
				'<a href="javascript:;" onclick="undo(\'' + tweetId + '\',\'' + owningUser + '\',\'' + ratingUser + '\',' + rtCount + ',' + newPoints + ')">[undo]</a>'
			);

			hideLoader();
		},
		'error': function(error) {
			alert('Error:' + error);
			hideLoader();
		}
	});
}

/*
 * Registers a thumbs-down rating asynchronously and modifies the points displayed
 * on the website accordingly. Moreover, changes the thumbs-up/down link to an
 * "undo" link.
 */
function thumbsDown(tweetId, owningUser, ratingUser, rtCount, points) {
	showLoader();

	$.ajax({
		'url': 'thumbs-down?id=' + tweetId + '&owning=' + owningUser + '&rating=' + ratingUser,
		'success': function(data) {
			var difference = parseInt(data);
			var newPoints = points + difference;
			var operator = '';
			var newPointsDisplay = newPoints;

			if (newPoints < 0) {
				operator = ' - ';
				newPointsDisplay *= -1;
			} else {
				operator = ' + ';
			}

			$('#rating_' + tweetId).html(rtCount + operator + newPointsDisplay + ' = ' + (rtCount + newPoints));
			$('#links_' + tweetId).html(
				'<span>You rated <strong>-1</strong> </span>' +
				'<a href="javascript:;" onclick="undo(\'' + tweetId + '\',\'' + owningUser + '\',\'' + ratingUser + '\',' + rtCount + ',' + newPoints + ')">[undo]</a>'
			);

			hideLoader();
		},
		'error': function() {
			alert('Error');
			hideLoader();
		}
	});
}

/*
 * Registers the revocation rating asynchronously and modifies the points displayed
 * on the website accordingly. Moreover, changes the "undo" link to a pair of thumbs-
 * up/down links.
 */
function undo(tweetId, owningUser, ratingUser, rtCount, points) {
	showLoader();

	$.ajax({
		'url': 'undo?id=' + tweetId + '&user=' + ratingUser,
		'success': function(data) {
			var difference = parseInt(data);
			var newPoints = points - difference;
			var operator = '';
			var newPointsDisplay = newPoints;

			if (newPoints < 0) {
				operator = ' - ';
				newPointsDisplay *= -1;
			} else {
				operator = ' + ';
			}

			$('#rating_' + tweetId).html(rtCount + operator + newPointsDisplay + ' = ' + (rtCount + newPoints));
			$('#links_' + tweetId).html(
				'<a href="javascript:;" onclick="thumbsUp(\'' + tweetId + '\',\'' + owningUser + '\',\'' + ratingUser + '\',' + rtCount + ',' + newPoints + ')"><img src="/thumbsup.png" border="0" /></a>' +
				'<a href="javascript:;" onclick="thumbsDown(\'' + tweetId + '\',\'' + owningUser + '\',\'' + ratingUser + '\',' + rtCount + ',' + newPoints + ')"><img src="/thumbsdown.png" border="0" /></a>'
			);

			hideLoader();
		},
		'error': function() {
			alert('Error');
			hideLoader();
		}
	});
}

/*
 * Shows the progress bar overlay.
 */
function showLoader() {
	$('#overlay').css('display', 'block');
	$('#loader').css('display', 'block');
}

/*
 * Hides the progress bar overlay.
 */
function hideLoader() {
	$('#overlay').css('display', 'none');
	$('#loader').css('display', 'none');
}